package cn.bonoon.controllers;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.batatasframework.rawdata.RawdataImportResult;
import org.batatasframework.rawdata.RawdataImportedResult;
import org.batatasframework.web.RequestParameterReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.bonoon.core.RawdataItem;
import cn.bonoon.core.RawdataStreamService;
import cn.bonoon.core.plugins.PlaceService;
import cn.bonoon.entities.PlaceLevel;
import cn.bonoon.kernel.events.PanelEvent;
import cn.bonoon.kernel.support.IOperator;
import cn.bonoon.kernel.web.controllers.AbstractPanelController;
import cn.bonoon.kernel.web.models.JsonResult;
import cn.bonoon.kernel.web.models.PanelModel;
/**
 * 行业数据
 * @author zjb
 *
 */
@Controller
@RequestMapping("s/rdi/{catalog}!import")
public class RawdataImportController extends AbstractPanelController{
	private @Autowired RawdataStreamService rawdataStreamService;
	private @Autowired PlaceService placeService;


	private String catalog;
	
	@ModelAttribute
	public void setCatalog(@PathVariable("catalog") String catalog) {
		this.catalog = catalog;
	}
	
	@Override
	protected void onLoad(PanelEvent event) throws Exception {
		
		/*
		 * 测试账号：U00664
		 */
		PanelModel model = event.getModel();
		IOperator user = event.getUser();
		List<RawdataItem> items = rawdataStreamService.myItems(user, catalog);
		PlaceLevel level=placeService.get(user.getOwnerId()).getLevel();
		items =rawdataStreamService.chooseItems(items, level,"R",user.getOwnerId());
		user.getOwnerId();
		model.addObject("items", items);
		model.addObject("catalog", catalog);
		
		event.setVmPath("plugs-rawdata/index");
	}
	
	@ResponseBody
	@RequestMapping(value = "!{key}/upload.do", method = RequestMethod.POST)
	public JsonResult upload(HttpServletRequest request, @PathVariable("key") String key){
		try{
			IOperator user = getUser();
			RequestParameterReader parameter = new RequestParameterReader(request);
			RawdataImportResult data = rawdataStreamService.executeUpload(user, catalog, parameter);
			return JsonResult.result(data);
		}catch (Exception e) {
			return JsonResult.error(e);
		}
	}

	@RequestMapping(value = "!{key}/result.do", method = RequestMethod.POST)
	public String loadResult(Model model, HttpServletRequest request, RawdataImportResult data){
		model.addAttribute("layout", "plugs-rawdata/layout-result.vm");
		try{
			IOperator user = getUser();
			RawdataImportedResult result = rawdataStreamService.importResult(user, catalog, data);
			model.addAttribute("result", result);
			model.addAttribute("items", result.getItems());
			return result.getVmpath();
		}catch (Exception e) {
			return getError(e);
		}
	}
}
